package com.itheima.tlias84.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias84.annotaion.Log;
import com.itheima.tlias84.dto.SelectAllEmpDTO;
import com.itheima.tlias84.dto.SelectEMPDetailDTO;
import com.itheima.tlias84.dto.SelectEmpDTO;
import com.itheima.tlias84.entity.Expr;
import com.itheima.tlias84.mapper.EmpMapper;
import com.itheima.tlias84.mapper.ExprMapper;
import com.itheima.tlias84.po.InsterEmpParam;
import com.itheima.tlias84.po.SelectEmpPageParam;
import com.itheima.tlias84.po.UpdateEmpParam;
import com.itheima.tlias84.service.EmpService;
import com.itheima.tlias84.utils.ResultUtil;
import com.itheima.tlias84.vo.PageVO;
import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {

    private static final Logger log = LoggerFactory.getLogger(EmpServiceImpl.class);
    @Resource
    EmpMapper empMapper;
    @Resource
    ExprMapper exprMapper;

    @Override
    public List<SelectAllEmpDTO> selectAll() {
        List<SelectAllEmpDTO> dtos = empMapper.selectAll();
        return dtos;
    }

    @Override
    public ResultUtil selectEmpByPage(SelectEmpPageParam param) {
        //计算分页
        //
        //param.setPage((param.getPage()-1)*param.getPageSize());
        //注意：pageHelper会自动执行一个count sql，并且会自动拼接limit
        //因此必须写在自己的sql的前面
        Page<Object> page = PageHelper.startPage(param.getPage(), param.getPageSize());
        List<SelectEmpDTO> dtos = empMapper.selectEmpByPage(param);
        PageVO<SelectEmpDTO> pageVO = new PageVO<>();
        pageVO.setTotal(page.getTotal());
        pageVO.setRows(dtos);
        return ResultUtil.success(pageVO);
    }

    @Log
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int batchDelete(List<Integer> ids) {
        try {
            //先删除员工
            int i = empMapper.batchDelete(ids);
            //再删除员工履历
            int i1 = exprMapper.batchdeleteExpr(ids);
            log.info("删除{}条履历",i1);
            System.out.println(1/0);
            return i;
        }catch (Exception e){
            log.error("发生异常",e);
            throw new RuntimeException("1");
        }

    }

    @Log
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertEmp(InsterEmpParam param) {
        //设置时间
        param.setCreateTime(LocalDateTime.now());
        param.setUpdateTime(LocalDateTime.now());
        int flag = empMapper.insertEmp(param);
        //插入工作履历
        log.info("员工回显的主键{}",param.getId());
        List<Expr> exprList =param.getExprList();
        if ( CollectionUtils.isNotEmpty(exprList)){
            //赋值员工id以及时间
            exprList = exprList.stream().map(t->{
                t.setEmpId(param.getId());
                t.setCreateTime(LocalDateTime.now());
                t.setUpdateTime(LocalDateTime.now());
                return t;
            }).toList();
            int i = exprMapper.batchInsertExpr(exprList);
            log.info("插入员工履历{}",i);
        }
        return flag;
    }

    @Override
    public SelectEMPDetailDTO selectEmpById(Integer id) {
        //先查询员工
        SelectEMPDetailDTO dto = empMapper.selectEmpById(id);
        //再查询员工履历
        List<Expr> exprs = exprMapper.selectExprByEmpId(id);
        dto.setExprList(exprs);
        return dto;
    }

    @Log
    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateEmp(UpdateEmpParam param) {
        param.setUpdateTime(LocalDateTime.now());
        //1.先更新员工表信息
        int empRows = empMapper.updateEmp(param);
        //2.再删除员工履历
        int exprDelRows = exprMapper.batchdeleteExpr(List.of(param.getId()));
        log.info("删除{}条旧履历数据",exprDelRows);
        //3.新增员工履历
        List<Expr> exprList =param.getExprList();
        if (CollectionUtils.isNotEmpty(exprList)){
            //赋值员工id以及时间
            exprList = exprList.stream().map(t->{
                t.setEmpId(param.getId());
                t.setCreateTime(LocalDateTime.now());
                t.setUpdateTime(LocalDateTime.now());
                return t;
            }).toList();
            int i = exprMapper.batchInsertExpr(exprList);
            log.info("插入{}条新员工履历",i);
        }
        return empRows;
    }
}
